package com.jtmg.glorykingsnews.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jtmg.glorykingsnews.domain.TbNotice;
import com.jtmg.glorykingsnews.vo.NoticeVo.NotiveQueryVo;
import com.jtmg.glorykingsnews.vo.commom.MsgVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.jtmg.glorykingsnews.mapper.TbNoticeMapper;
import com.jtmg.glorykingsnews.service.TbNoticeService;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class TbNoticeServiceImpl implements TbNoticeService{

    @Resource
    private TbNoticeMapper tbNoticeMapper;

    @Override
    public PageInfo<TbNotice> queryNotice(NotiveQueryVo query) {
        String title = query.getTitle();
        String detail = query.getDetail();
        Example example = new Example(TbNotice.class);
        if (query.getTitle() != null && !"".equals(query.getTitle())){
            example.createCriteria().andLike("title","%"+title+"%");
        }
        if (query.getDetail() != null && !"".equals(query.getDetail())){
            example.createCriteria().andLike("title","%"+detail+"%");
        }
        PageHelper.startPage(query.getPage(), query.getLimit());
        return new PageInfo<>(tbNoticeMapper.selectByExample(example));
    }

    @Override
    public MsgVo createNotice(NotiveQueryVo query) {
        try{
            TbNotice notice = new TbNotice();
            notice.setTitle(query.getTitle());
            notice.setDetail(query.getDetail());
            notice.setnTime(new Date());
            notice.setDeleted(0);
            tbNoticeMapper.insert(notice);
            return new MsgVo("200","新增公告信息成功");
        }catch (Exception e){
            return new MsgVo("201","新增公告信息异常");
        }
    }

    @Override
    public MsgVo deleteNotice(int id) {
        try {
            tbNoticeMapper.deleteByPrimaryKey(id);
            return new MsgVo("200","删除公告信息成功");
        }catch (Exception e){
            return new MsgVo("201","删除公告信息失败");
        }
    }
}
